import sql from '@/utils/sqlLite.js'
import {
	setObjSql,
	obj2str_sql
} from '@/utils/index.js'
// 数据表名称  产品类型
export const deviceType = 'deviceType'
export const database = uni.getStorageSync('database')
/*
productId 产品的pid  
deviceTypeName 产品类型名字
deviceImg 产品图片
tenantable 可租数量
reserve 预定
inStore 在库数量
leaseRatio 租赁率


productModel 品类
manufacturer
brand
modelDesc
deviceImg 产品图片
tenantable 可租数量
reserve 预定
inStore 在库数量
leaseRatio 租赁率

*/
export const crateDeviceTypeTable = async () => {

	let sqltxt = [
		`create table if not exists  ${deviceType}("productModel" VARCHAR(24),"manufacturer" VARCHAR(24),"brand" VARCHAR(24),"modelDesc" VARCHAR(24),"deviceImg" VARCHAR(128),"tenantable" int(8),"reserve" int(8),"inStore" int(8),"leaseRatio" int(3))`
	];
	let res = await sql.executeSQL(uni.getStorageSync('database'), sqltxt);
	console.log(res);
}

// 添加设备类型
export const addDeviceType = async (pushData) => {
	console.log(pushData.productModel);
	// 数据库中productModel 存在 修改 ，不存在 新增
	let sqltxt = `select *from ${deviceType} where productModel = '${pushData.productModel}'`;
	let res = await sql.selectSQL(uni.getStorageSync('database'), sqltxt);
	console.log(res);
	if (res.code == 0 && res.data.length == 0) { //数据库中没有数据 新增

		let condition = Object.keys(pushData).join(',')

		let data = setObjSql(pushData)
		console.log('data', data);
		let sqltxt = `INSERT  INTO ${deviceType} (${condition}) VALUES(${data})`
		// console.log(condition, data);
		return await sql.executeSQL(uni.getStorageSync('database'), sqltxt);
	} else { //存在 ，更改

		let data = obj2str_sql(pushData)
		let sqltxt = `update  ${deviceType} set ${data} where productModel = '${pushData.productModel}'`
		return  await sql.executeSQL(uni.getStorageSync('database'), sqltxt);
	}
}

// 获取全部的设备类型
export const getDeviceType = async () => {
	let sqltxt = `select * from ${deviceType}`;
	let res = await sql.selectSQL(uni.getStorageSync('database'), sqltxt);
	return res

}


// 获取类型详情 productModel
export const getDevModel = async (productModel) => {
	let sqltxt = `select * from ${deviceType} where productModel = '${productModel}'`;
	return await sql.selectSQL(uni.getStorageSync('database'), sqltxt);
}

// 设备计数器自动加一
export const reduceInsoter = async (productModel) => {
	let sqltxt = `update ${deviceType} set inStore=inStore-1  where productModel = '${productModel}'`;

	return await sql.selectSQL(uni.getStorageSync('database'), sqltxt);
}

export const reduceTenantable = async (productModel) => {
	let sqltxt = `update ${deviceType} set tenantable=tenantable-1  where productModel = '${productModel}'`;

	return await sql.selectSQL(uni.getStorageSync('database'), sqltxt);
}


export const addInsoter = async (productModel) => {
	let sqltxt = `update ${deviceType} set inStore=inStore+1  where productModel = '${productModel}'`;

	return await sql.selectSQL(uni.getStorageSync('database'), sqltxt);
}

export const addTenantable = async (productModel) => {
	let sqltxt = `update ${deviceType} set tenantable=tenantable+1  where productModel = '${productModel}'`;

	return await sql.selectSQL(uni.getStorageSync('database'), sqltxt);
}